package com.cb.domain.model.user;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

import com.cb.domain.model.system.AppRole;


/**
 * Model class for User. Add not database mapped fileds in this class.
 */
@Entity
@Table(name="user_account")
public class UserAccount implements UserDetails{
	
	protected Long userId;
	protected String email;
	protected String username;
	protected String password;
	protected Short status;
	protected String iconPhoto;
	protected String fullname;
	protected java.util.Date birthday;
	protected String phone;
	protected String mobile;
	protected Short sex;
	protected java.util.Date createTime;
	protected Short userType;
	protected java.util.Set<AppRole> roles = new java.util.HashSet();
	protected String memberCard; //会员卡号
	protected String idCard; //身份证号
	protected String address; //联系地址
	protected String postCode;//邮政编码
	
	/**
	 * 用户状态status
	 * 0=禁用
	 * 
	 */
	public static Short FORBIDDEN_STATUS=0;
	/**
	 * 用户状态status
	 * 1=启用
	 */
	public static Short ACTIVATION_STATUS=1;
	/**
	 * 超管用户
	 */
	public static Short USER_TYPE_SUPER_ADMIN=1;
	/**
	 * 企业会员
	 */
	public static Short USER_TYPE_ENTERPRISE_USER=2;
	/**
	 * 普通用户
	 */
	public static Short USER_TYPE_MEMBER=1;

	@Transient
	public boolean isCorrectPwd(String password) {
		if (StringUtils.isBlank(this.password))
			throw new UnsupportedOperationException("password is unloaded");
		return this.password.equals(password);
	}
	
	/**
	 * 为登录用户加上一个登录后可以访问的角色
	 */
	@Transient
	public GrantedAuthority[] getAuthorities() {
		GrantedAuthority[] granted=(GrantedAuthority[]) roles.toArray(new GrantedAuthority[roles.size()+2]);
//		if(USER_TYPE_SUPER_ADMIN.equals(userType)){//企业用户
//			granted[granted.length-2]=new GrantedAuthorityImpl(AppRole.ROLE_ADMINISTRATOR);
//		}else if(USER_TYPE_MEMBER.equals(userType)){//个人用户
//			granted[granted.length-2]=new GrantedAuthorityImpl(AppRole.ROLE_MEMBER);
//		}
		granted[granted.length-2]=new GrantedAuthorityImpl(AppRole.ROLE_PUBLIC);
		granted[granted.length-1]=new GrantedAuthorityImpl(AppRole.ROLE_MEMBER);
		return granted;
	}

	@Transient
	public synchronized boolean isCredentialsNonExpired() {
		return true;
	}

	@Transient
	public boolean isAccountNonExpired() {
		return true;
	}

	@Transient
	public boolean isAccountNonLocked() {
		return true;
	}

	@Transient
	public boolean isEnabled() {
		return ACTIVATION_STATUS.equals(status);
	}

	@Id     
    @GeneratedValue(generator="Id_Generator")
    @GenericGenerator(name="Id_Generator", strategy="com.cb.common.util.IdGenerator")   
	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	@Column(name = "email")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	@Column(name = "username")
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "password")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "status")
	public Short getStatus() {
		return status;
	}

	public void setStatus(Short status) {
		this.status = status;
	}

	@Column(name = "iconPhoto")
	public String getIconPhoto() {
		return iconPhoto;
	}

	public void setIconPhoto(String iconPhoto) {
		this.iconPhoto = iconPhoto;
	}

	@Column(name = "fullname")
	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	@Column(name = "birthday")
	public java.util.Date getBirthday() {
		return birthday;
	}

	public void setBirthday(java.util.Date birthday) {
		this.birthday = birthday;
	}

	@Column(name = "phone")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Column(name = "mobile")
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	@Column(name = "sex")
	public Short getSex() {
		return sex;
	}

	public void setSex(Short sex) {
		this.sex = sex;
	}

	@Column(name = "createTime")
	public java.util.Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(java.util.Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "userType")
	public Short getUserType() {
		return userType;
	}

	public void setUserType(Short userType) {
		this.userType = userType;
	}

	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="user_role",joinColumns={@JoinColumn(name="userId")},inverseJoinColumns={@JoinColumn(name="roleId")})
	public java.util.Set<AppRole> getRoles() {
		return roles;
	}

	public void setRoles(java.util.Set<AppRole> roles) {
		this.roles = roles;
	}
	
	@Column(name = "memberCard")
	public String getMemberCard() {
		return memberCard;
	}

	public void setMemberCard(String memberCard) {
		this.memberCard = memberCard;
	}
	
	@Column(name = "idCard")
	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}
	
	@Column(name = "address")
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	@Column(name = "postCode")
	public String getPostCode() {
		return postCode;
	}

	public void setPostCode(String postCode) {
		this.postCode = postCode;
	}
	
	
}
